package com.aaa.controller;

import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.entity.Dept;
import com.aaa.entity.MyReturn;
import com.aaa.entity.PageSort;
import com.aaa.service.DeptService;
import com.aaa.utils.Constants;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * 部门表(Dept)表控制层
 *
 * @author makejava
 * @since 2020-12-17 10:26:20
 */
@RestController
@RequestMapping("dept")

public class DeptController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;

    private MyReturn myReturn = new MyReturn();

    /**
     * 页面跳转
     *
     * @return ModelAndView
     * @TeacherChen
     * @createTime 2020/12/30 22:36
     */
    @GetMapping("toShowDept.do")
    public ModelAndView toDept() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("showDept");
        return modelAndView;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne.do")
    public Dept selectOne(Integer id) {
        return this.deptService.queryById(id);
    }

    /**
     * 按条件查询dept表并分页
     *
     * @param pageSort 分页参数
     * @return ReturnBean
     * @TeacherChen
     * @createTime 2020/12/29 22:40
     */

    @GetMapping("selectAll.do")
    public MyReturn selectAll(PageSort pageSort) {
     /*   //使用cglib动态代理，实现日志增强处理
            //获取代理对象
        ProxyDynamicCGlibImpl<DeptServiceImpl> proxyDynamicCGlibImpl = new ProxyDynamicCGlibImpl<DeptServiceImpl>(deptService);
        DeptService proxyObj = proxyDynamicCGlibImpl.getProxyObj();*/
        //使用代理对象执行目标方法
        Page page = deptService.findAllDept(pageSort);
        // Page page = proxyObj.findAllDept(pageSort);
        //获取分页之后的数据
        myReturn.setData(page.getResult());
        //获取总条数
        myReturn.setCount(page.getTotal());
        return super.getSuccessResult(myReturn);
    }

    /**
     * 删除操作
     *
     * @param deptId 主键
     * @return ReturnBean
     * @TeacherChen
     * @createTime 2020/12/29 22:41
     */

    @RequestMapping("deleteById.do")
    public MyReturn deleteById(Integer deptId) {
        String[] id = new String[]{String.valueOf(deptId)};
        boolean check = deptService.checkNotNull(id);
        //true:部门有员工
        if (check) {
            getFailResult(myReturn);
            myReturn.setMsg(Constants.DEPT_NOT_NULL);
            return myReturn;
        } else {
            //开始删除
            boolean delete = deptService.deleteById(deptId);
            if (delete) {
                return super.getSuccessResult(myReturn);
            }
            return super.getFailResult(myReturn);
        }
    }

    /**
     * 多选删除
     *
     * @param ids 需要删除的主键
     * @return ReturnBean
     * @TeacherChen
     * @createTime 2020/12/29 22:42
     */

    @RequestMapping("deleteByIds.do")
    public MyReturn deleteByIds(String ids) {
        boolean checkNotNull = deptService.checkNotNull(ids.split(","));
        if (checkNotNull) {
            getFailResult(myReturn);
            myReturn.setMsg(Constants.DEPT_NOT_NULL);
            return myReturn;
        } else {
            boolean delete = deptService.deleteByIds(ids.split(","));
            if (delete) {
                return getSuccessResult(myReturn);
            }
            return getFailResult(myReturn);
        }
    }

    /**
     * @param dept:
     * @return MyReturn
     * @author W
     * @description 判断重名
     * @date 2022/7/15 15:47
     */
    @RequestMapping("findDeptName.do")
    public MyReturn findDeptName(Dept dept) {
        boolean check = deptService.findDeptName(dept);
        //true:重名
        if (check) {
            getFailResult(myReturn);
            myReturn.setMsg(Constants.DEPT_NAME_EXIST);
            return myReturn;
        }
        return getSuccessResult(myReturn);
    }

    /**
     * 更新操作
     *
     * @param dept 实体类
     * @return ReturnBean
     * @TeacherChen
     * @createTime 2020/12/29 22:43
     */

    @RequestMapping("updateDept.do")
    @UpdateAnn
    public MyReturn updateDept(Dept dept) {
        System.out.println(dept.toString());
        try {
            Dept update = deptService.update(dept);
            myReturn.setData(update);
            return getSuccessResult(myReturn);
        } catch (Exception e) {
            return getFailResult(myReturn);
        }
    }

    /**
     * 增加操作
     *
     * @param dept 实体类
     * @return ReturnBean
     * @TeacherChen
     * @createTime 2020/12/29 22:45
     */

    @PostMapping("insertDept.do")
    @InsertAnn
    public MyReturn insertDept(Dept dept) {
        dept.setDelFlag("0");
        try {
            Dept insertDept = deptService.insert(dept);
            myReturn.setData(insertDept);
            return getSuccessResult(myReturn);
        } catch (Exception e) {
            return getFailResult(null);
        }
    }
}
